﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;

namespace EverydayTips
{
    [Serializable]
    class Tip
    {
        public Tip(Guid uid,string title,string source,string keywords,string content,DateTime dt)
        {
            this.uID = uid;
            this.title = title;
            this.source = source;
            this.keywords = keywords;
            this.content = content;
            this.date = dt;
            this.passwd = "";
            this.locked = false;
        }
        public Tip(Guid uid, string title, string source, string keywords, string content, DateTime dt,bool islock,string password)
        {
            this.uID = uid;
            this.title = title;
            this.source = source;
            this.keywords = keywords;
            this.content = content;
            this.date = dt;
            this.passwd = Util.getMd5Hash(password);
            this.locked = islock;
        }
        public string getMd5Passwd()
        {
            return this.passwd;
        }
        public void SetMd5Passwd(string md5password)
        {
            this.locked = true;
            this.passwd = md5password;
        }
        public static void save(Tip t)
        {
            string fileName = t.uID.ToString();
            string dir = "data\\"+fileName.Substring(0,1)+"\\"+fileName.Substring(1,1);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string path = dir + "\\" + fileName;
            FileStream fs = new FileStream(path, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, t);
            fs.Close();
        }
        public static  Tip fetch(Guid uid)
        {
            Tip t = null;
            string fileName = uid.ToString();
            string path = "data\\" + fileName.Substring(0, 1) + "\\" + fileName.Substring(1, 1) + "\\" + fileName;
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryFormatter bf = new BinaryFormatter();
            t = (Tip)bf.Deserialize(fs);
            fs.Close();
            return t;
        }
        public static void delete(Guid uid)
        {
            string fileName = uid.ToString();
            string path = "data\\" + fileName.Substring(0, 1) + "\\" + fileName.Substring(1, 1) + "\\" + fileName;
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        
        public Guid uID;
        public string title;
        public string source;
        public string keywords;
        public string content;
        public DateTime date;
        public bool locked;
        private string passwd;
    }
}
